import json

# json是一个数据格式，.json文件就是存放json数据的

"""对文件的操作"""
# 读取json数据
# with open('./demo.json','r') as f:
#     json_data = json.load(f) # load() 是将json数据转换成python对象
#     print(type(json_data)) # <class 'dict'>
# with open('./demo.json','r') as f:
#     json_data = f.read()
#     print(type(json_data))  # <class 'str'>


# 将python对象（dict）写入到json文件中
data = {
    'name':'DengZe',
    'address':'SY'
}
# with open('./demo.json','w') as f:
#     json.dump(data,f,indent=1)
# with open('./demo.json','w') as f: # 错误的写入
#     f.write(str(data))

"""对数据的操作"""
data1 = {
    'name':'DengZe',
    'address':'SY'
}
# 将字典转换成json格式的字符串
json_data = json.dumps(data1, indent=1)
print(json_data) # <class 'str'>

# 将json格式字符串转换成字典
py_data = json.loads(json_data)
print(py_data) # {'name': 'DengZe', 'address': 'SY'} <class 'dict'>